package com.atmilan.quartz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.atmilan.quartz.mapper.StoreOrderInfoMapper;
import com.atmilan.quartz.model.entity.crmeb.StoreOrderInfo;
import com.atmilan.quartz.model.result.crmeb.StoreCartResponse;
import com.atmilan.quartz.model.vo.crmeb.StoreOrderInfoVo;
import com.atmilan.quartz.service.IStoreOrderInfoService;
import com.atmilan.quartz.service.IStoreProductReplyService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
* @author Mr.Zhang
* @description StoreOrderInfoServiceImpl 接口实现
* @date 2020-05-28
*/
@Service
public class StoreOrderInfoServiceImpl extends ServiceImpl<StoreOrderInfoMapper, StoreOrderInfo> implements IStoreOrderInfoService {
    @Autowired
    private IStoreProductReplyService storeProductReplyService;
    
    
    /**
     * 根据id集合查询数据，返回 map
     * @param orderId Integer id
     * @author Mr.Zhang
     * @since 2020-04-17
     * @return HashMap<Integer, StoreCart>
     */
    @Override
    public List<StoreOrderInfoVo> getOrderListByOrderId(Integer orderId) {
        // 通过订单id查询所有详情
        List<StoreOrderInfo> systemStoreStaffList = list(Wrappers.<StoreOrderInfo>lambdaQuery()
                .eq(StoreOrderInfo :: getOrderId, orderId)
                );
        if (CollectionUtils.isEmpty(systemStoreStaffList)) {
            return null;
        }

        List<StoreOrderInfoVo> storeOrderInfoVoList = new ArrayList<StoreOrderInfoVo>();
        for (StoreOrderInfo storeOrderInfo : systemStoreStaffList) {
            //解析商品详情JSON
            StoreOrderInfoVo StoreOrderInfoVo = new StoreOrderInfoVo();
            BeanUtils.copyProperties(storeOrderInfo, StoreOrderInfoVo, "info");
            StoreOrderInfoVo.setInfo(JSON.parseObject(storeOrderInfo.getInfo(), StoreCartResponse.class));
            StoreOrderInfoVo.getInfo().setIsReply(storeProductReplyService.isReply(StoreOrderInfoVo.getUnique(), "product", StoreOrderInfoVo.getOrderId()).size());
            storeOrderInfoVoList.add(StoreOrderInfoVo);
        }
        
        return storeOrderInfoVoList;
    }
    
}
